﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace Ocmf.Types
{
    public class Trial : Action
    {
        private string alias = string.Empty;
        public string Alias
        {
            get
            {
                return alias;
            }
            set
            {
                alias = value;
                Text = "Trial " + Index + (alias != string.Empty ? " (" + alias + ")" : string.Empty);
            }
        }
        
        public Trial()
        {
            Text = "Trial " + Index + (alias != string.Empty ? " (" + alias + ")" : string.Empty);
            SelectedImageKey = ImageKey = "trial";
        }

        public override object Clone()
        {
            var clone = base.Clone() as Trial;
            return clone;
        }

        public override Form Editor
        {
            get { throw new NotImplementedException(); }
        }

        public override XmlElement ToXml(XmlDocument xmlDocument)
        {
            //create trial xml element
            var trialElement = xmlDocument.CreateElement("Trial");
            //create, set and append text attribute
            var trialTextAttribute = xmlDocument.CreateAttribute("Alias");
            trialTextAttribute.InnerText = alias;
            trialElement.Attributes.Append(trialTextAttribute);
            //iterate over each action
            foreach (Action action in Nodes)
                trialElement.AppendChild(action.ToXml(xmlDocument));
            //return element
            return trialElement;
        }

        public override void FromXml(XmlNode xmlElement)
        {
            alias = xmlElement.Attributes["Alias"].Value;
            foreach (XmlNode xmlNode in xmlElement.ChildNodes)
            {
                Action action = null;
                switch(xmlNode.Name)
                {
                    case "Folder":
                        action = new Folder();
                        break;
                    case "Card":
                        action = new Card();
                        break;
                    case "Buzzer":
                        action = new Buzzer();
                        break;
                    case "Food":
                        action = new Food();
                        break;
                    case "GoTo":
                        action = new GoTo();
                        break;
                    case "HouseLight":
                        action = new HouseLight();
                        break;
                    case "Prl":
                        action = new Prl();
                        break;
                    case "Stop":
                        action = new Stop();
                        break;
                    case "Delay":
                        action = new Delay();
                        break;
                    case "Counter":
                        action = new Counter();
                        break;
                }
                
                action.FromXml(xmlNode);
                Nodes.Add(action);
            }
        }
    }
}
